import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.MinioException;
import io.minio.messages.Item;

public class MinioTest {
   static MinioClient minioClient;

    static {
        try {
            minioClient = new MinioClient("http://127.0.0.1:9000", "minioadmin", "minioadmin");
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        } catch (InvalidPortException e) {
            e.printStackTrace();
        }
    }

    public MinioTest() throws InvalidPortException, InvalidEndpointException {
    }

    public static void main(String[] args) {
        //查询
        try {
            // 检查'mybucket'是否存在。
            boolean found = minioClient.bucketExists("test");
            if (found) {
                // 列出'my-bucketname'里的对象
//                Iterable<Result<Item>> myObjects = minioClient.listObjects("test");
                 Iterable<Result<Item>> myObjects = minioClient.listObjects( "test",  "test1", true,  false);


                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    System.out.println(item.lastModified() + ", " + item.objectSize() + ", " + item.objectName()+","+item.storageClass());
                }
            } else {
                System.out.println("mybucket does not exist");
            }
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        //下载
//        try {
//            // 调用statObject()来判断对象是否存在。
//            // 如果不存在, statObject()抛出异常,
//            // 否则则代表对象存在。
//            minioClient.statObject("test", "test1/ceshi/ngrok-stable-windows-amd64.zip");
//
//            // 获取myobject的流并保存到photo.jpg文件中。
//            minioClient.getObject("test", "test1/ceshi/ngrok-stable-windows-amd64.zip","D://test.zip");
//
//        } catch (Exception e) {
//            System.out.println("Error occurred: " + e);
//        }
//        try {
//            minioClient.putObject("test",  "island.zip", "D://test.zip");
//            System.out.println("island.jpg is uploaded successfully");
//        } catch(Exception e) {
//            System.out.println("Error occurred: " + e);
//        }
    }
}
